package com.seek.eduservice.controller;


import com.alibaba.excel.EasyExcel;
import com.seek.commonutils.ResponseResult;
import com.seek.eduservice.entity.excel.SubjectData;
import com.seek.eduservice.entity.subject.Subject;
import com.seek.eduservice.listener.SubjectExcelListener;
import com.seek.eduservice.service.EduSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author wml
 * @since 2022-01-15
 */
@RestController
@RequestMapping("/eduservice/edu-subject")
public class EduSubjectController {

    @Autowired
    private EduSubjectService eduSubjectService;

    // 添加课程分类
    // 获取上传过来的文件，把文件内容读取出来
    @PostMapping("add")
    public ResponseResult add(MultipartFile file) {
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 调用方法进行读取
        SubjectExcelListener subjectExcelListener = new SubjectExcelListener();
        EasyExcel.read(inputStream, SubjectData.class, subjectExcelListener).sheet().doRead();
        if (subjectExcelListener.list.size() == 0) {
            return ResponseResult.ok().message("文件数据为空");
        }
        eduSubjectService.saveSubject(subjectExcelListener.list);
        return ResponseResult.ok().message("导入成功");
    }

    // 课程分类列表（树形）
    @GetMapping("getAll")
    public ResponseResult getAll() {
        List<Subject> list = eduSubjectService.getAll();
        return ResponseResult.ok().data("list", list);
    }

}

